package com.lagou.edu;


import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Activate(group = {CommonConstants.PROVIDER,CommonConstants.CONSUMER})
public class TransportIPFilter implements Filter {
    public static final ThreadLocal<String> THREAD_LOCAL=new ThreadLocal<>();
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        RpcContext context = RpcContext.getContext();
        if(context.isConsumerSide()){
            String remoteHost = context.getRemoteHost();
            int remotePort = context.getRemotePort();
            context.setAttachment("remoteHost_Port",remoteHost+":"+remotePort);

            String threadLocalRemote = THREAD_LOCAL.get();
            context.setAttachment("threadLocalRemote",threadLocalRemote);

        }else if(context.isProviderSide()){
            Object o1 = context.getAttachment("remoteHost_Port");
            System.out.println("过滤器打印：remoteHost_Port =>"+o1);

            Object threadLocalRemote = context.getAttachment("threadLocalRemote");
            System.out.println("过滤器打印：threadLocalRemote =>"+threadLocalRemote);

            Object controllerRemoteAddr = context.getAttachment("controllerRemoteAddr");
            System.out.println("过滤器打印：controllerRemoteAddr =>"+controllerRemoteAddr);
        }
        return  invoker.invoke(invocation);
    }

}
